package eerbaar.v2.app.tasks;

import java.util.ArrayList;
import java.util.List;

import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.app.mover.PersonaMover;

public class Global {

    interface IPersonaFiler {
        boolean canInclude(IPersona persona);
    }

    public static void createMovers(List<PersonaMover> movers,
            IPersonaFiler filter) {

        IGame game = GameHelper.getGame();
        if (game == null) {
            return;
        }

        List<IPersona> list = new ArrayList<IPersona>();

        IPersona[] personas = game.getPersonas();
        for (IPersona p : personas) {

            if (filter.canInclude(p) == false) {
                continue;
            }

            PersonaMover m = new PersonaMover(p);
            if (!movers.contains(m)) {
                m.waitForStart();
                movers.add(m);
            }
            list.add(p);
        }

        List<PersonaMover> remove = new ArrayList<PersonaMover>();
        for (PersonaMover m : movers) {
            if (!list.contains(m)) {
                remove.add(m);
            }
        }

        for (PersonaMover m : remove) {
            movers.remove(m);
        }

    }
}
